package com.coffee.takeoutserver.controller;

import com.coffee.takeoutserver.bean.MyCollection;
import com.coffee.takeoutserver.bean.Seller;
import com.coffee.takeoutserver.service.CollectionService;
import com.coffee.takeoutserver.service.SellerService;
import com.coffee.takeoutserver.util.RequestType;
import com.coffee.takeoutserver.util.ResponseInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CollectionController {


    @Autowired
    CollectionService collectionService;
    @Autowired
    SellerService sellerService;

    @RequestMapping("collect")
    @ResponseBody
    public String collect(int userId,int sellerId) {
        Gson gson = new Gson();
        MyCollection collection = collectionService.getCollectionByUserId(userId);
        if(collection==null){
            collection = new MyCollection();
            collection.setId(null);
            collection.setUserid(userId);
            List<Integer> list = new ArrayList<>();
            list.add(sellerId);
            collection.setSelleridlist(gson.toJson(list));
            collectionService.addCollection(collection);
        }else if(collection.getSelleridlist()==null||collection.getSelleridlist().isEmpty()){
            List<Integer> list = new ArrayList<>();
            list.add(sellerId);
            collection.setSelleridlist(gson.toJson(list));
            collectionService.updateCollectionByUserId(collection);
        }
        else{
            List<Integer> sellerIdList = gson.fromJson(collection.getSelleridlist(), new TypeToken<List<Integer>>(){}.getType());
            sellerIdList.add(sellerId);
            collection.setSelleridlist(gson.toJson(sellerIdList));
            collectionService.updateCollectionByUserId(collection);
        }
        Map<String,String> map = new HashMap<>();
        map.put("requestType",RequestType.Collection_Collect +"");
        map.put("info","success");
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        responseInfo.setData(gson.toJson(map));;
        return gson.toJson(responseInfo);
    }

@RequestMapping("getCollectionByUserId")
@ResponseBody
public String getCollectionByUserId(int userId) {
    Gson gson = new Gson();
    MyCollection collection = collectionService.getCollectionByUserId(userId);
    String sellerListStr = collection.getSelleridlist();
    ResponseInfo responseInfo = new ResponseInfo();
    responseInfo.setCode("0");
    if(null==sellerListStr||sellerListStr.equals("")){
        return gson.toJson(responseInfo);
    }
    List<Integer> sellerIdList = gson.fromJson(sellerListStr, new TypeToken<List<Integer>>(){}.getType());
    List<Seller> sellerList = sellerService.getSellerListBySellerIdList(sellerIdList);
    Map<String,String> map = new HashMap<>();
    map.put("requestType", RequestType.Collection_GetCollectionByUserId +"");
    map.put("sellerList",gson.toJson(sellerList));
    responseInfo.setData(gson.toJson(map));
    return gson.toJson(responseInfo);
}

    @RequestMapping("cancelCollect")
    @ResponseBody
    public String cancelCollect(int userId,int sellerId) {
        Gson gson = new Gson();
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        Map<String,String> map = new HashMap<>();
        map.put("requestType",RequestType.Collection_CancelCollect);
        map.put("info","success");
        responseInfo.setData(gson.toJson(map));
        MyCollection collection = collectionService.getCollectionByUserId(userId);
        if(collection==null||collection.getSelleridlist()==null||collection.getSelleridlist().isEmpty()){
            return gson.toJson(responseInfo);
        }
        List<Integer> sellerIdList = gson.fromJson(collection.getSelleridlist(), new TypeToken<List<Integer>>(){}.getType());
        for(int i=0;i<sellerIdList.size();i++){
            if(sellerIdList.get(i)==sellerId){
                sellerIdList.remove(i);
                i--;
            }
        }
        collection.setSelleridlist(gson.toJson(sellerIdList));
        collectionService.updateCollectionByUserId(collection);
        return gson.toJson(responseInfo);
    }

    @RequestMapping("isCollected")
    @ResponseBody
    public String isCollected(int userId,int sellerId) {
        Gson gson = new Gson();
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        Map<String,String> map = new HashMap<>();
        map.put("requestType",RequestType.Collection_IsCollected);
        MyCollection collection = collectionService.getCollectionByUserId(userId);
        if(collection==null||collection.getSelleridlist()==null||collection.getSelleridlist().isEmpty()){
            map.put("info","no");
            responseInfo.setData(gson.toJson(map));
            return gson.toJson(responseInfo);
        }
        List<Integer> sellerIdList = gson.fromJson(collection.getSelleridlist(), new TypeToken<List<Integer>>(){}.getType());
        for(int i=0;i<sellerIdList.size();i++){
            if(sellerIdList.get(i)==sellerId){
                map.put("info","yes");
                responseInfo.setData(gson.toJson(map));
                return gson.toJson(responseInfo);
            }
        }
        map.put("info","no");
        responseInfo.setData(gson.toJson(map));
        return gson.toJson(responseInfo);
    }
}
